package com.mason.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-10-02
 * Time: 0:11
 */
public class APPResult<T> {

    @JsonInclude(JsonInclude.Include.ALWAYS)//保证一定会被序列化
    private int code;//状态码
    @JsonInclude(JsonInclude.Include.ALWAYS)//保证一定会被序列化
    private String message;//描述语
    @JsonInclude(JsonInclude.Include.ALWAYS)//保证一定会被序列化
    private T data;//具体数据

    // 对外提供一些静态的工厂方法

    // 1. 成功相关  -----------------------------
    public static APPResult success() {
        return new APPResult(ResultCode.SUCCESS);
    }
    public static APPResult success(String message) {
        return new APPResult(ResultCode.SUCCESS.getCode(), message);
    }
    public static <T> APPResult<T> success(T data) {
        return new APPResult(ResultCode.SUCCESS, data);
    }
    public static <T> APPResult<T> success(String message, T data) {
        return new APPResult(ResultCode.SUCCESS.getCode(), message, data);
    }
    // 2. 失败相关  -----------------------------
    public static APPResult failed() {
        return new APPResult(ResultCode.FAILED);
    }
    public static APPResult failed(String message) {
        return new APPResult(ResultCode.FAILED.getCode(), message);
    }
    public static APPResult failed(ResultCode resultCode) {
        return new APPResult(resultCode);
    }

    // 3. 重定向相关 -----------------------------
    public static <T> APPResult<T> redirect(T data) {
        return new APPResult(ResultCode.FAILED_FORBIDDEN, data);
    }


// -------------[构造方法、getter方法、setter方法]------------------
    public APPResult(ResultCode resultCode) {
        this(resultCode.getCode(), resultCode.getMessage());
    }
    public APPResult(ResultCode resultCode, T data) {
        this(resultCode.getCode(), resultCode.getMessage(), data);
    }


    public APPResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public APPResult(int code, String message) {
        this.code = code;
        this.message = message;
    }
    public APPResult() {
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
// ---------------------------------------------------
}
